import java.util.LinkedList;
import java.util.Queue;

public class MyStackBett {
    Queue<Integer> queueA = new LinkedList<>();
    Queue<Integer> queueB = new LinkedList<>();

    public MyStackBett() {}

    public void push(int x) {
        if (queueA.isEmpty()) {
            queueB.offer(x);  //如果A为空,B入队
        } else {
            queueA.offer(x);
        }
    }

    public int pop() {
        int ret = 0;
        if (queueA.isEmpty()) {
            Queue<Integer> tmp = queueA;
            queueA = queueB;
            queueB = tmp;
        }
        while (queueA.size() > 0) {
            if (queueA.size() != 1) {
                //如果不是目标元素，则出队进入a队列中
                queueB.offer(queueA.poll());
            } else {
                ret = queueA.poll();
            }
        }
        return ret;
    }

    public int top() {
        int ret = 0;
        if (queueA.isEmpty()) {
            if (queueA.isEmpty()) {
                Queue<Integer> tmp = queueA;
                queueA = queueB;
                queueB = tmp;
            }
            while (queueA.size() > 0) {
                if (queueA.size() != 1) {
                    //如果不是目标元素，则出队进入a队列中
                    queueB.offer(queueA.poll());
                } else {
                    ret = queueA.poll();
                    queueB.offer(ret);
                }
            }
        }
        return ret;
    }

    public boolean empty() {
        return queueA.isEmpty() && queueB.isEmpty();
    }
}
